package com.bookstore.service.impl;

import com.bookstore.entity.BookCar;
import com.bookstore.entity.PageBean;
import com.bookstore.mapper.BookCarMapper;
import com.bookstore.service.BookCarService;
import com.bookstore.tools.CreateSqlSession;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author :Cofe
 * @Date 2022/4/21 19:44
 */
@Service
public class BookCarServiceImpl implements BookCarService {
    @Resource
    private BookCarMapper bcm;

    @Override
    public void addBookCar(int bid, String book_name,double book_balance,int user_uid,String book_pic,double book_membalance) {
        bcm.insertBookCar(bid,book_name,book_balance,user_uid,book_pic,book_membalance);
    }

    @Override
    public List<BookCar> findcarbyid(int user_uid) {
        return bcm.findcarbyuid(user_uid);
    }

    @Override
    public void deleteAll(int user_uid) {
        bcm.daleteAllinfo(user_uid);
    }

    @Override
    public boolean deleteByBuyId(int buy_id) {
        Integer integer = bcm.deleteByBuy(buy_id);
        return integer>0;       //如果删除成功则为true 失败则为false
    }

    @Override
    public BookCar findBybuy_id(int buy_Id) {
        BookCar findcarbyid = bcm.findcarbyid(buy_Id);
        return findcarbyid;
    }

    @Override
    public PageBean<BookCar> selectByPage(int currentPage, int pageSize,int user_uid) {
        int begin = (currentPage - 1) * pageSize;
        // 计算查询条目数
        int size = pageSize;
        //5. 查询当前页数据
        List<BookCar> rows = bcm.selectByPage(begin, size,user_uid);
        //6. 查询总记录数
        System.out.println(rows);
        int totalCount = bcm.selectTotalCount();
        //7. 封装PageBean对象
        PageBean<BookCar> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);
        return pageBean;
    }

    @Override
    public PageBean<BookCar> selectByPageAndCondition(int currentPage, int pageSize, BookCar bookCar) {
        //计算开始索引
        int begin = (currentPage - 1) * pageSize;
        // 计算查询条目数
        int size = pageSize;
        // 处理brand条件，模糊表达式
        int bookID = bookCar.getBid();
        if (bookID > 0) {
            bookCar.setBid(bookID);
        }
        int bid=bookCar.getBid();
        if (bid > 0) {
            bookCar.setBid(bid);
        }
        String bookName = bookCar.getBook_name();
        if (bookName != null && bookName.length() > 0) {
            bookCar.setBook_name("%" + bookName + "%");
        }
        //5. 查询当前页数据
        List<BookCar> rows = bcm.selectByPageAndCondition(begin, size, bookCar);
        //6. 查询总记录数
        int totalCount = bcm.selectTotalCountByCondition(bookCar);
        //7. 封装PageBean对象
        PageBean<BookCar> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);
        return pageBean;
    }
}
